import { Component, ChangeDetectorRef, ChangeDetectionStrategy, Input } from '@angular/core';
import { OverviewModel } from '../../templates/overview/ratingOverview.model';
import { HttpClient } from '@angular/common/http';
import { UtilsService } from '../../../../../../shared/services/utils.service';
import { CompanyRiskRatingService } from '../../companyRiskRating.service';
import { Subscription } from 'rxjs';
import { ChartRiskFrameworkModel } from '../../templates/overview/models/chartRiskFramework.model';
import { CompanyService } from '../../../company.service';
import { ResearchState } from '../../../../research.service';
import { ClusterService } from '../../../../riskCluster/cluster.service';

/**
 * Company overview summary component
 *
 * Author: Sorin Spiru
 * Date: 16.01.2018
 */
@Component({
    selector: 'company-rating-overview',
    templateUrl: '../../templates/overview/ratingOverview.component.html',
    styleUrls: [
        '../../../company.component.scss',
        '../../templates/overview/ratingOverview.component.scss'
    ],
    providers: [UtilsService],
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class RatingOverviewSummaryComponent extends OverviewModel {
    private sub: Subscription;
    public company;
    public mei;

    @Input() displayType = 'overview';

    constructor(
        private cdRef: ChangeDetectorRef,
        private riskService: CompanyRiskRatingService,
        private companyService: CompanyService,
        private clusterService: ClusterService) {
        super(cdRef);
    }

    ngOnInit() {
        this.loaded = new Promise<void>((resolve, reject) => {
            this.companyService.company.subscribe(data => {
                this.isCore = data.coreRiskResearchState === ResearchState.ResearchAvailable;

                // clusters
                this.clusterService.company.subscribe(clusters => {
                    this.riskClusters = clusters;
                    this.sub = this.riskService.company.subscribe(company => {
                        this.company = company;
                        this.lastUpdate = company.lastUpdate;
                        this.riskRating = undefined;
                        this.cdRef.markForCheck();

                        if (!this.isCore) {
                            this.currentClusterCode = company.assessmentCode;

                            var cluster = this.riskClusters.filter(function(cluster){
                                return cluster.code == company.assessmentCode;
                            })[0];

                            this.currentClusterName = cluster != undefined ? cluster.name : "N/A";
                        } else {
                            this.riskClusters.forEach(cluster => {
                                if (company['assessment'] === cluster.name) {
                                    this.currentClusterCode = cluster.code;
                                    this.currentClusterName = cluster.name;
                                }
                            });
                        }

                        // rating chart
                        this.riskRating = UtilsService.roundUp(company.riskRating);

                        // exposure chart
                        this.riskFramework = [
                            new ChartRiskFrameworkModel("Exposure", 0, company.exposure, company.exposure, "#B3D4D1"),
                            new ChartRiskFrameworkModel("Managed Risk", 0, company.managedRisk, company.managedRisk, "#A7A9AC"),
                            new ChartRiskFrameworkModel("Overall Unmanaged Risk", company.managedRisk, company.exposure, company.riskRating, "#000000")];
                        this.cdRef.markForCheck();
                        resolve();
                    });
                });
            });
        });
    }

    ngOnDestroy(){
        this.sub.unsubscribe();
    }
}